import { BrowserWindow } from "electron";
import path from "node:path";
import isDev from "electron-is-dev";
import { join } from "path";

export default function createAddStockWindow() {
  const addStockWindow = new BrowserWindow({
    width: 400,
    height: 600,
    type: "toolbar", // 创建的窗口类型为工具栏窗口
    frame: true, // 要创建无边框窗口
    titleBarStyle: "hidden",
    resizable: false,
    alwaysOnTop: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: true, // very import,对于多窗口，一定要隔离，不然preload乱了就，接收不到消息
      preload: path.join(__dirname, "preload.js"),
    },
  });

  const port = process.env.PORT || 3000;
  const url = isDev
    ? `http://localhost:${port}/addStock.html`
    : join(__dirname, "../src/out/addStock.html");

  // and load the index.html of the app.
  if (isDev) {
    addStockWindow?.loadURL(url);
  } else {
    addStockWindow?.loadFile(url);
  }
}
